package com.inifitness.tenant.core.system.controller.biz;

import java.util.List;

import com.inifitness.common.model.Option;
import com.inifitness.common.result.Result;
import com.inifitness.tenant.core.config.resubmit.annotation.PreventDuplicateSubmit;
import com.inifitness.tenant.core.system.model.form.BizMenuForm;
import com.inifitness.tenant.core.system.model.query.BizMenuQuery;
import com.inifitness.tenant.core.system.model.vo.BizMenuVO;
import com.inifitness.tenant.core.system.model.vo.BizRouteVO;
import com.inifitness.tenant.core.system.service.BizMenuService;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j2;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 菜单控制器
 *
 * @author sunjinfeng
 * @since 2020/11/06
 */
@Tag(name = "04.菜单接口")
@RestController
@RequestMapping("/api/v1/menus")
@RequiredArgsConstructor
@Log4j2
public class BizMenuController {

  private final BizMenuService menuService;

  @Operation(summary = "菜单列表")
  @GetMapping
  public Result<List<BizMenuVO>> listMenus(@ParameterObject BizMenuQuery queryParams) {
    List<BizMenuVO> menuList = menuService.listMenus(queryParams);
    return Result.success(menuList);
  }

  @Operation(summary = "菜单下拉列表")
  @GetMapping("/options")
  public Result listMenuOptions() {
    List<Option> menus = menuService.listMenuOptions();
    return Result.success(menus);
  }

  @Operation(summary = "路由列表")
  @GetMapping("/routes")
  public Result<List<BizRouteVO>> listRoutes() {
    List<BizRouteVO> routeList = menuService.listRoutes();
    return Result.success(routeList);
  }

  @Operation(summary = "菜单表单数据")
  @GetMapping("/{id}/form")
  public Result<BizMenuForm> getMenuForm(
      @Parameter(description = "菜单ID") @PathVariable(value = "id") Long id
  ) {
    BizMenuForm menu = menuService.getMenuForm(id);
    return Result.success(menu);
  }

  @Operation(summary = "新增菜单")
  @PostMapping
  @PreAuthorize("@ss.hasPerm('sys:menu:add')")
  @PreventDuplicateSubmit
  public Result addMenu(@RequestBody BizMenuForm bizMenuForm) {
    boolean result = menuService.saveMenu(bizMenuForm);
    return Result.judge(result);
  }

  @Operation(summary = "修改菜单")
  @PutMapping(value = "/{id}")
  @PreAuthorize("@ss.hasPerm('sys:menu:edit')")
  public Result updateMenu(@PathVariable(value = "id") Long id,
      @RequestBody BizMenuForm bizMenuForm
  ) {
    boolean result = menuService.saveMenu(bizMenuForm);
    return Result.judge(result);
  }

  @Operation(summary = "删除菜单")
  @DeleteMapping("/{id}")
  @PreAuthorize("@ss.hasPerm('sys:menu:delete')")
  public Result deleteMenu(
      @Parameter(description = "菜单ID，多个以英文(,)分割") @PathVariable("id") Long id
  ) {
    boolean result = menuService.deleteMenu(id);
    return Result.judge(result);
  }

  @Operation(summary = "修改菜单显示状态")
  @PatchMapping("/{menuId}")
  public Result updateMenuVisible(
      @Parameter(description = "菜单ID") @PathVariable(value = "menuId") Long menuId,
      @Parameter(description = "显示状态(1:显示;0:隐藏)") Integer visible

  ) {
    boolean result = menuService.updateMenuVisible(menuId, visible);
    return Result.judge(result);
  }


}

